/******************************************************************************
;	FIFO.h - Header file for Queue code
;	Copyright (c) 2011 Matthew Villarrubia, Yves Santos. All rights reserved.
;	Any use of this software without prior written notice is prohibitted.
******************************************************************************/
#ifndef __FIFO_H__
#define __FIFO_H__

#include<string.h>
#include<stdlib.h>

typedef struct {
	unsigned char* volatile getPtr;		//Pointer to the start of the fifo
	unsigned char* volatile putPtr;		//Pointer to the end of the fifo
	unsigned char* startDataPtr;		//Pointer to the start of the fifo data in memory
	unsigned char* endDataPtr;			//Pointer to the end of the fifo data in memory
	size_t elementSize;					//Size of one element in the fifo
}_FIFO;

typedef _FIFO* _tFIFO;

//*****************************************************************************
//	Creates a circular FIFO. FIFOs are not thread safe. Use semaphores or
//	critical sections for thread safety.
//! \param fifo_capacity This is the maximum number of elements that can be
//! stored in this FIFO.
//! \param size This is the size (in bytes) of a single element in the FIFO.
//!	For portability reasons, use sizeof() for this paramater. Ex: To create a
//!	FIFO of 10 ints: FIFO_CreateFIFO(10,sizeof(int));
//! \return On success, returns a pointer to a FIFO. NULL otherwise.
//*****************************************************************************
_tFIFO
FIFO_CreateFifo(unsigned int fifo_capacity, size_t size);
//*****************************************************************************
//	Puts a single element into the FIFO.
//! \param fifo Pointer to the FIFO to store the data in
//! \param data Data to be stored in the FIFO
//!	\return	Returns 0 on success. Returns -1 otherwise
//*****************************************************************************
int
FIFO_PutSingle(_tFIFO fifo, void* data);

//*****************************************************************************
//	Pulls data off a FIFO.
//! \param fifo Pointer to the FIFO to retrieve the data from
//! \param data Pointer to data destination
//!	\return	Returns 0 on success. Returns -1 otherwise
//*****************************************************************************
int
FIFO_GetSingle(_tFIFO fifo, void* data);

//*****************************************************************************
//	POPs the last element off the FIFO and discards it. Exactly the same as POP
//	in a stack.
//! \param fifo Pointer to the FIFO to delete the data from
//!	\return	Returns 0 on success. Returns -1 otherwise
//*****************************************************************************
int
FIFO_DeleteLastEntry(_tFIFO fifo);

//*****************************************************************************
//! \param fifo Pointer to the FIFO to retrieve the data from
//!	\return	Returns number of available spaces in the FIFO
//*****************************************************************************
unsigned int
FIFO_GetNumAvailableSpaces(_tFIFO fifo);

//*****************************************************************************
//! \param fifo Pointer to the FIFO to retrieve the data from
//!	\return	Returns the number of elements stored in the FIFO
//*****************************************************************************
unsigned int
FIFO_GetSize(_tFIFO fifo);

//*****************************************************************************
//	Frees the memory associated with the provided FIFO
//! \param fifo Pointer to the FIFO that will be deleted
//!	\return	None
//*****************************************************************************
void FIFO_Free(_tFIFO fifo);

#endif
